package pATT.bNEdit.gui;

import java.util.Enumeration;
import java.util.Vector;

import javax.swing.table.AbstractTableModel;

import pATT.bNEdit.base.ExperienceTable;
import pATT.bNEdit.base.Nodo;
import pATT.bNEdit.base.RedBayes;
import pATT.bNEdit.inference.BucketElimination;
import pATT.bNEdit.learning.FractionalUpdating;


/**
 * <p>Title: BNEdit</p>
 * <p>Description: Adapter for the class ExperienceTable. Allows it to be
* shown and edited inside a JTable </p>
 * <p>Copyright: Copyright (c) 2004</p>
 * <p>Company: </p>
 * @author Luciana Zavaleta
 * @version 1.0
 */

public class ExperienceTableModel extends AbstractTableModel {

  /**
	 * 
	 */
	private static final long serialVersionUID = 1L;
private Nodo node;

  public ExperienceTableModel(Nodo node) {
    this.node = node;
    node.addExperienceTable();
  }

  public void setValueAt(Object aValue, int rowIndex, int columnIndex) {
    if (columnIndex!=0 && columnIndex<getColumnCount()) {
      columnIndex--;
      node.getExperienceTable().set(columnIndex, ( (Double) aValue).doubleValue());
   }
  }

  public String getColumnName(int columnIndex) {
    if (columnIndex>0) {
      return getStates(columnIndex).toString();
    }
    else
      return "State";
  }

  public boolean isCellEditable(int rowIndex, int columnIndex) {
    return true;
  }

  public Object getValueAt(int rowIndex, int columnIndex) {
   if (columnIndex>0 && columnIndex<getColumnCount()){
     columnIndex--;
     return new Double(node.getExperienceTable().get(columnIndex));
   }
   return "Exp. Count";
  }

  public int getColumnCount() {
   int n=1;
   Enumeration e=node.getProbabilidadOriginal().getDominio().getEstados().elements();
   e.nextElement();
   while (e.hasMoreElements()){
     n=n*((Integer)e.nextElement()).intValue();
   }
   return n +1;
  }

  public int getRowCount() {
    return 1;
  }

  @SuppressWarnings("unchecked")
private Vector getStates(int columnIndex) {
    Vector states = new Vector();
    int size = getColumnCount()-1;
    if (columnIndex>0) {
      columnIndex--;
      Enumeration enumStates = node.getProbabilidadOriginal().getDominio().getEstados().elements();
      Enumeration enumParents = node.getPadres().elements();
      enumStates.nextElement();
      while (enumStates.hasMoreElements()) {
        int n = ((Integer)enumStates.nextElement()).intValue();
        size /= n;
        int stateIndex = columnIndex / size;
        Nodo parent = (Nodo) enumParents.nextElement();
        states.add(parent.getNombreEstado(stateIndex));
        columnIndex %= size;
      }
    }
    return states;
  }

  public void incrementExp (int index,double exp){
    node.getExperienceTable().increment(index,exp);
  }

  @SuppressWarnings("unchecked")
public Class getColumnClass(int columnIndex) {
    if (columnIndex>0)
      return Double.class;
    else
      return String.class;
  }

  public ExperienceTable getExperienceTable(){
    return node.getExperienceTable();
  }

  public void update(RedBayes bayesianNetwork){
    FractionalUpdating fractionalUpdate=new FractionalUpdating();
    fractionalUpdate.adapt(bayesianNetwork, new BucketElimination());
  }

}
